//
//  AdressBookTableViewController.m
//  UI_通讯录.通讯录.
//
//  Created by 男哥呵呵 on 16/1/21.
//  Copyright © 2016年 Mr.x. All rights reserved.
//

#import "AdressBookTableViewController.h"
#import "AdressBookModel.h"
#import "AdressTableViewCell.h"
#import "ViewController.h"
#import "ShowViewController.h"

@interface AdressBookTableViewController ()<ViewControllerDelegate>

@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation AdressBookTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(rightAction:)];
    
    self.dataSource = [NSMutableArray arrayWithCapacity:0];
    AdressBookModel *adressModel = [[AdressBookModel alloc] init];
    adressModel.name = @"王建男";
    adressModel.gender = @"迷";
    adressModel.phoneNum = @"120";
    adressModel.adress =@"精神病院2号床";
    adressModel.image = [UIImage imageNamed:@"1"];
    [self.dataSource addObject:adressModel];
    
    [self.tableView registerClass:[AdressTableViewCell class] forCellReuseIdentifier:@"AdressTableViewCellIdentifier"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 添加
- (void)rightAction:(UIBarButtonItem *)sender {
    ViewController *vc = [[ViewController alloc] init];
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)sendModel:(AdressBookModel *)model {
    [self.dataSource addObject:model];
    [self.tableView reloadData];
}

#pragma mark - Table view data source

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Incomplete implementation, return the number of sections
//    return 0;
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete implementation, return the number of rows
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AdressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AdressTableViewCellIdentifier" forIndexPath:indexPath];
    
    cell.model = self.dataSource[indexPath.row];
    
    return cell;
}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.dataSource removeObjectAtIndex:indexPath.row];
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    ShowViewController *vc = [[ShowViewController alloc] init];
    vc.showModel = self.dataSource[indexPath.row];
    [self presentViewController:vc animated:YES completion:nil];
}

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
